<?php



/**
 * Represents a single chatlog.
 */
class Chatlog {
	private $protocol;		// The protocol for the conversation.
	private $account;		// The account on the protocol.
	private $recipient;		// The recipient of the conversation.
	private $startDate;		// The startDate of the conversation.
	private $revision = 0;	// Serverside revision (version) of the log.
	private $contents;		// Contents of the log.
	
	
	
	/**
	 * @return the $protocol
	 */
	public function getProtocol() {
		return $this->protocol;
	}
	/**
	 * @return the $account
	 */
	public function getAccount() {
		return $this->account;
	}
	/**
	 * @return the $recipient
	 */
	public function getRecipient() {
		return $this->recipient;
	}
	/**
	 * @return the $startDate
	 */
	public function getStartDate() {
		return $this->startDate;
	}
	/**
	 * @return the $revision
	 */
	public function getRevision() {
		return $this->revision;
	}
	/**
	 * @return Contents of the log.
	 */
	public function getContents() {
		return $this->contents;
	}
	/**
	 * @param protocol Protocol to set.
	 */
	public function setProtocol($protocol) {
		$this->protocol = $protocol;
	}
	/**
	 * @param account	Account to set.
	 */
	public function setAccount($account) {
		$this->account = $account;
	}
	/**
	 * @param recipient	Recipient to set.
	 */
	public function setRecipient($recipient) {
		$this->recipient = $recipient;
	}
	/**
	 * @param startDate	StartDate to set.
	 */
	public function setStartDate($startDate) {
		$this->startDate = $startDate;
	}
	/**
	 * @param revision	Revision to set.
	 */
	public function setRevision($revision) {
		$this->revision = $revision;
	}
	/**
	 * @param contents Contents to set.
	 */
	public function setContents($contents) {
		$this->contents = $contents;
	}
	
	
	
	/**
	 * Adds the given string to the contents of the chatlog.
	 * 
	 * @param contents	The contents to add.
	 */
	public function addContents($contents) {
		$this->contents .= $contents;
	}
	
	
	
	/**
	 * Clears the contents of the chatlog.
	 */
	public function clearContents() {
		$this->contents = "";
	}
	
	
	
	/**
	 * Merges two chatlogs.
	 * 	The current version of this function just takes the largest log of the two.
	 * 
	 * @param log	The log to merge with.
	 * @return True if the contents were changed, False otherwise.
	 */
	public function merge(Chatlog $log) {
		// Check input:
		if (!$log) {
			return false;
		}
		
		// Which one is the largest?
		Logger::log("Chatlog", "Merging logs.");
		$ownSize = strlen($this->getContents());
		$newSize = strlen($log->getContents());
		$result = false;
		if ($newSize > $ownSize) {
			// Update contents:
			$this->setContents($log->getContents());
			
			// Update revision:
			$this->setRevision($this->getRevision() + 1);
			$result = true;
		}
		Logger::log("Chatlog", "Logs merged.");
		
		// Return result:
		return $result;
	}
	
	
	
	/**
	 * Returns the XML representation of the log.
	 * 
	 * @return XML string representing this log.
	 */
	public function toXML() {
		return "<log protocol=\"" . $this->getProtocol() . "\" account=\"" . $this->getAccount() . "\" recipient=\"" . $this->getRecipient() . "\" startDate=\"" . $this->getStartDate() . "\" revision=\"" . $this->getRevision() ."\"/>";
	}
}
?>
